﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DSheet.Core.Data;
using DSheet.Core;

namespace DExcelServiceApp
{
    public partial class DimDocTemplateList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (DBContext context = new DBContext())
                {
                    List<DocDef> datas = DocDefService.GetAllDocDef(context);
                    _ctlGridDoc.DataSource = datas;
                    _ctlGridDoc.DataBind();
                }
            }
        }

        protected void _ctlBtnCreateDoc_Click(object sender, EventArgs e)
        {
            DocDef dimdata = new DocDef();
            dimdata.Id = Guid.NewGuid();
            dimdata.Code = _ctlEditCode.Text.Trim();
            dimdata.Name = _ctlEditName.Text.Trim();

            using (DBContext context = new DBContext())
            {
                DocDefService.SaveDocDef(context, dimdata);

                List<DocDef> datas = DocDefService.GetAllDocDef(context);
                _ctlGridDoc.DataSource = datas;
                _ctlGridDoc.DataBind();
            }
        }

        protected void _ctlGridDoc_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (DBContext context = new DBContext())
            {
                Guid docId = new Guid(_ctlGridDoc.SelectedValue.ToString());
                List<DimData> datas = new DimDataService().GetAllDims(context);
                _ctlGridDims.DataSource = datas;
                _ctlGridDims.DataBind();

                DocDef docDef = DocDefService.GetDocDef(context, docId);
                _ctlListAssignedYDims.DataSource = docDef.YDims;
                _ctlListAssignedYDims.DataBind();
                _ctlListAssignedXDims.DataSource = docDef.XDims;
                _ctlListAssignedXDims.DataBind();
                _ctlListPageDims.DataSource = docDef.GetPageDims();
                _ctlListPageDims.DataBind();

                _ctlHiddenDocId.Value = docId.ToString();
                _ctlLinkTemplateList.NavigateUrl = string.Format("./DocTemplateList.aspx?id={0}", _ctlHiddenDocId.Value);
            }
        }

        protected void _ctlGridDims_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid dimId = new Guid(e.CommandArgument.ToString());
            Guid docId = new Guid(_ctlHiddenDocId.Value);
            if (e.CommandName == "AssignY")
            {
                using (DBContext context = new DBContext())
                {
                    DimData dimData = new DimDataService().GetDimData(context, dimId);

                    DocDef docDef = DocDefService.GetDocDef(context, docId);

                    foreach (var dimItem in docDef.YDims)
                    {
                        if (dimItem.DimId == dimId)
                        {
                            return;
                        }
                    }
                    foreach (var dimItem in docDef.XDims)
                    {
                        if (dimItem.DimId == dimId)
                        {
                            return;
                        }
                    }

                    DocDim docDim = new DocDim();
                    docDim.Id = Guid.NewGuid();
                    docDim.DimId = dimId;
                    docDim.DimCode = dimData.Code;
                    docDim.DimName = dimData.Name;
                    docDim.Direction = DimDirection.Y;
                    docDef.YDims.Add(docDim);
                    DocDefService.SaveDocDims(context, docDef);

                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                }
            }
            else
            {
                using (DBContext context = new DBContext())
                {
                    DimData dimData = new DimDataService().GetDimData(context, dimId);

                    DocDef docDef = DocDefService.GetDocDef(context, docId);

                    foreach (var dimItem in docDef.YDims)
                    {
                        if (dimItem.DimId == dimId)
                        {
                            return;
                        }
                    }
                    foreach (var dimItem in docDef.XDims)
                    {
                        if (dimItem.DimId == dimId)
                        {
                            return;
                        }
                    }

                    DocDim docDim = new DocDim();
                    docDim.Id = Guid.NewGuid();
                    docDim.DimId = dimId;
                    docDim.DimCode = dimData.Code;
                    docDim.DimName = dimData.Name;
                    docDim.Direction = DimDirection.X;
                    docDef.XDims.Add(docDim);
                    DocDefService.SaveDocDims(context, docDef);

                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                }
            }
        }

        protected void _ctlListAssignedYDims_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid docId = new Guid(_ctlHiddenDocId.Value);
            Guid docDimId = new Guid(e.CommandArgument.ToString());
            if (e.CommandName == "Up")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.Up(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                }
            }
            else if (e.CommandName == "Down")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.Down(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                }
            }
            else if (e.CommandName == "SetX")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.SetXDirection(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                }
            }
            else if (e.CommandName == "SetPD")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.SetPageDim(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                    _ctlListPageDims.DataSource = docDef.GetPageDims();
                    _ctlListPageDims.DataBind();
                }
            }
        }

        protected void _ctlListAssignedXDims_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid docId = new Guid(_ctlHiddenDocId.Value);
            Guid docDimId = new Guid(e.CommandArgument.ToString());
            if (e.CommandName == "Up")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.Up(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                }
            }
            else if (e.CommandName == "Down")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.Down(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                }
            }
            else if (e.CommandName == "SetY")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.SetYDirection(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                }
            }
            else if (e.CommandName == "SetPD")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.SetPageDim(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListAssignedYDims.DataSource = docDef.YDims;
                    _ctlListAssignedYDims.DataBind();
                    _ctlListAssignedXDims.DataSource = docDef.XDims;
                    _ctlListAssignedXDims.DataBind();
                    _ctlListPageDims.DataSource = docDef.GetPageDims();
                    _ctlListPageDims.DataBind();
                }
            }
        }

        protected void _ctlListAssignedPageDims_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            Guid docId = new Guid(_ctlHiddenDocId.Value);
            Guid docDimId = new Guid(e.CommandArgument.ToString());
            if (e.CommandName == "Clear")
            {
                using (DBContext context = new DBContext())
                {
                    DocDef docDef = DocDefService.GetDocDef(context, docId);
                    docDef.ClearPageDim(docDimId);
                    DocDefService.SaveDocDims(context, docDef);
                    docDef = DocDefService.GetDocDef(context, docId);
                    _ctlListPageDims.DataSource = docDef.GetPageDims();
                    _ctlListPageDims.DataBind();
                }
            }
        }

        protected void _ctlBtnPreview_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("./DocDefPreview.aspx?docid={0}", _ctlHiddenDocId.Value));
        }
    }
}